package com.bappi.custom.activities;

import java.util.Stack;

import com.weighttracker.android.R;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;

public class CustomActivityGroup extends ActivityGroup {
	protected Stack<String> history;
	private View currentView;

	private static int index = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.history = new Stack<String>();
	}

	public void pushView(View nextView, String id) {
		if (history.size() > 0) {
			nextView.startAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_to_middle));
			currentView.startAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_middle_to_left));
		}
		history.add(id);
		currentView = nextView;
		setContentView(nextView);
	}

	public void pushIntent(Intent intent) {
		pushIntent(intent, null);
	}

	public void pushIntent(Intent intent, Object iData) {
		String id = getClass().getCanonicalName() + (++index);
		View view = getLocalActivityManager().startActivity(id,
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
		if (iData != null) {
			Activity activity = getLocalActivityManager().getActivity(id);
			if (activity instanceof CustomActivityInterface) {
				((CustomActivityInterface) activity).intentDataSent(iData);
			}
		}
		pushView(view, id);
	}

	public void back() {
		if (history.size() > 1) {
			String lastId = history.peek();
			history.pop();
			Activity resumingActivity = getLocalActivityManager().getActivity(
					history.peek());

			View nextView = resumingActivity.getWindow().getDecorView();
			currentView.startAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_middle_to_right));
			nextView.startAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_to_middle));
			currentView = nextView;
			Activity finishingActivity = getLocalActivityManager().getActivity(
					lastId);
			if ((finishingActivity instanceof CustomActivityInterface) && (resumingActivity instanceof CustomActivityInterface)) {
				CustomActivityInterface cai = (CustomActivityInterface) finishingActivity;
				((CustomActivityInterface)resumingActivity).willResume(cai.getResultResult());
			}
			setContentView(nextView);
			if (finishingActivity instanceof CustomActivityInterface) {
				((CustomActivityInterface) finishingActivity).willFinish();
			}
			getLocalActivityManager().destroyActivity(lastId, true);
		} else {
			super.finish();
		}
	}

	public void forceBack() {
		while (history.size() > 1) {
			String lastId = history.peek();
			history.pop();
			getLocalActivityManager().destroyActivity(lastId, true);
		}
		if (getParent() instanceof CustomTabActivity) {
			((CustomTabActivity) getParent()).exit();
		}
	}

	@Override
	public void finish() {
		back();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		try {
			super.onConfigurationChanged(newConfig);
			if (history.size() > 1) {
				Activity currentActivity = getLocalActivityManager()
						.getActivity(history.peek());
				if (currentActivity instanceof CustomActivity) {
					((CustomActivity) currentActivity)
							.orientstionChanged(newConfig);
				} else if (currentActivity instanceof CustomListActivity) {
					((CustomListActivity) currentActivity)
							.orientstionChanged(newConfig);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (history.size() > 0) {
			Activity resumingActivity = getLocalActivityManager().getActivity(
					history.peek());
			if (resumingActivity instanceof CustomActivityInterface) {
				((CustomActivityInterface) resumingActivity).onActivityResult(
						requestCode, resultCode, data, this);
			} 
		}
	}
}
